package com.cao.controller;


import com.cao.entity.Test;
import com.cao.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EN
 * @since 2021-08-25
 */
@Controller
@RequestMapping("test.do")
public class TestController {

    @Autowired
    private TestMapper testMapper;

    //添加
    @RequestMapping(params = "method=add")
    public void getAdd(){
        Test test=new Test();
        test.setTitle("测试增加");
        test.setTimg("1.jpg");
        int result=testMapper.insert(test);
        System.out.println(test);
    }

    //全查询
    @RequestMapping(params = "method=all")
    @ResponseBody
    public List<Test>  getAll(){
         InetAddress myhost=null;
            try {
                myhost=InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }

            //得到被服务器的地址
        String xpath=myhost.getHostAddress();

        List<Test> testList= testMapper.selectList(null);

        for (Test tt:testList
             ) {
            //拼接地址
            tt.setTimg("http://"+xpath+":9000/"+tt.getTimg());
            System.out.println(tt.getTimg());
        }
        return testList;
    }
}

